package com.xxs.controller.admin;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;

import com.jfinal.aop.Before;
import com.xxs.common.Const;
import com.xxs.interceptor.AdminInterceptor;
import com.xxs.utils.UUIDUtils;
import com.xxs.utils.xml.Fields;
import com.xxs.utils.xml.XmlDom4jUtils;

@Before(AdminInterceptor.class)
@SuppressWarnings("unchecked")
public class TableController extends BaseAdminController {
	
    public void listTables(){
    	List<Fields> fieldList = XmlDom4jUtils.getFields(Const.SYSTEM_TABLES_PATH,getModeName());
        setAttr("fieldList", fieldList);
		Element element = XmlDom4jUtils.getRootElement(Const.SYSTEM_TABLES_PATH);
    	List<Element> list = element.elements();
    	setAttr("list", list);
    	render("listTables.html");
    }
    public void listFields(){
    	String id = getPara(0);
    	String nodeName = getPara(1);
    	List<Fields> fieldList = XmlDom4jUtils.getFields(Const.SYSTEM_ATTR_PATH,null);
    	String modelName = getAttrForStr("modelName");
    	Document document = XmlDom4jUtils.getDocument(Const.SYSTEM_TABLES_PATH);
    	Element element = null;
		List<Element> listElements = document.selectNodes("/"+modelName+"s/"+nodeName+"[@id='"+id+"']");
		if(listElements!=null && listElements.size()>0){
    		element = listElements.get(0);
    	}
    	setAttr("fieldList", fieldList);
    	setAttr("elements", element);
    	setAttr("nodeName", nodeName);
		setAttr("id", id);
    	render("listFields.html");
    }
    public void addTables(){
    	render("addTables.html");
    }
    public void addFields(){
    	String id = getPara(0);
    	String name = getPara(1);
    	List<Fields> fieldList = XmlDom4jUtils.getFields(Const.SYSTEM_ATTR_PATH,null);
    	setAttr("id", id);
    	setAttr("name", name);
        setAttr("fieldList", fieldList);
    	render("addFields.html");
    }
	public void editTables(){
    	String id = getPara(0);
    	String name = getPara(1);
    	Document document = XmlDom4jUtils.getDocument(Const.SYSTEM_TABLES_PATH);
    	Element element = null;
		List<Element> listElements = document.selectNodes("//"+name+"[@id='"+id+"']");
		if(listElements!=null && listElements.size()>0){
    		element = listElements.get(0);
    	}else{
    		renderText("出现错误了，根据ID未检索到内容");
    	}
		setAttr("element", element);
    	setAttr("name", name);
    	render("editTables.html");
    }
    public void saveFields(){
    	String id = getPara(0);
    	String name = getPara(1);
    	String nodename = getPara("nodename");
    	String modelName = getAttrForStr("modelName");
    	List<Fields> fieldList = XmlDom4jUtils.getFields(Const.SYSTEM_ATTR_PATH,null);
    	Document document = XmlDom4jUtils.getDocument(Const.SYSTEM_TABLES_PATH);
    	Element element = null;
		List<Element> listElements = document.selectNodes("/"+modelName+"s/"+name+"[@id='"+id+"']");
		if(listElements!=null && listElements.size()>0){
    		element = listElements.get(0);
    	}else{
    		renderText("出现错误了，根据ID未检索到内容");
    	}
        //添加节点  
        Element newElement = element.addElement(nodename);  
        //设置节点名称
        newElement.setName(nodename);
        for (int i = 0; i < fieldList.size(); i++) {
			System.out.println("当前循环的属性名："+fieldList.get(i).getName()+"填充的内容"+getPara(fieldList.get(i).getName()));
			if("id".equals(fieldList.get(i).getName())){
				newElement.addAttribute(fieldList.get(i).getName(), UUIDUtils.creatUUID());
			}else{
				newElement.addAttribute(fieldList.get(i).getName(), getPara(fieldList.get(i).getName()));
			}
		}
		try {
			XmlDom4jUtils.saveDocumentToFile(Const.SYSTEM_TABLES_PATH, document);
		} catch (Exception e) {
			e.printStackTrace();
		}
        listTables();
    }
    public void saveTables(){
    	String nodename = getPara("nodename");
        String modelName = getAttrForStr("modelName");
		List<Fields> fieldList = getFields();
		Document document = XmlDom4jUtils.getDocument(Const.SYSTEM_TABLES_PATH);
		Element element = document.getRootElement();
		Element newElement = element.addElement(modelName);
		//设置节点名称 
		newElement.setName(nodename);
		for (int i = 0; i < fieldList.size(); i++) {
			System.out.println("当前循环的属性名："+fieldList.get(i).getName()+"填充的内容"+getPara(fieldList.get(i).getName()));
			if("id".equals(fieldList.get(i).getName())){
				newElement.addAttribute(fieldList.get(i).getName(), UUIDUtils.creatUUID());
			}else{
				newElement.addAttribute(fieldList.get(i).getName(), getPara(fieldList.get(i).getName()));
			}
		}
		try {
			XmlDom4jUtils.saveDocumentToFile(Const.SYSTEM_TABLES_PATH, document);
		} catch (Exception e) {
			e.printStackTrace();
		}
        listTables();
    }
	public void editFields(){
		String id = getPara(0);
    	String name = getPara(1);
    	Document document = XmlDom4jUtils.getDocument(Const.SYSTEM_TABLES_PATH);
    	Element element = null;
		List<Element> listElements = document.selectNodes("//"+name+"[@id='"+id+"']");
		if(listElements!=null && listElements.size()>0){
    		element = listElements.get(0);
    	}else{
    		renderText("出现错误了，根据ID未检索到内容");
    	}
    	
        List<Attribute> list = element.attributes();  
        Map<String, String> map = new HashMap<String, String>();
		//遍历属性节点  
		for(Attribute attribute : list){  
			System.out.println("属性"+attribute.getName() +":" + attribute.getValue());  
			map.put(attribute.getName(), attribute.getValue());
		} 
		setAttr("element", element);
    	setAttr("id", id);
    	setAttr("name", name);
    	render("editFields.html");
    }
    public void updateTables(){
    	String v0 = getPara(0);
    	Document document = XmlDom4jUtils.getDocument(Const.SYSTEM_TABLES_PATH);
    	Element element = document.getRootElement();  
    	element = element.element(v0);
    	Enumeration<String> enumeration = getParaNames();
    	while(enumeration.hasMoreElements()){
    		String name=(String)enumeration.nextElement();
    		String value=getPara(name);
    		System.out.print(name + "=" + value);
    		Attribute attribute = element.attribute(name);  
    		attribute.setValue(value);
    	}
    	try {
    		XmlDom4jUtils.saveDocumentToFile(Const.SYSTEM_TABLES_PATH, document);
    	} catch (Exception e) {
    		e.printStackTrace();
    	}
    	listTables();
    }
    public void updateFields(){
    	String v0 = getPara(0);
    	String v1 = getPara(1);
    	Document document = XmlDom4jUtils.getDocument(Const.SYSTEM_TABLES_PATH);
    	Element element = document.getRootElement();  
    	element = element.element(v0);
    	element = element.element(v1);
    	Enumeration<String> enumeration = getParaNames();
    	while(enumeration.hasMoreElements()){
    		String name=(String)enumeration.nextElement();
    		String value=getPara(name);
    		System.out.print(name + "=" + value);
    		Attribute attribute = element.attribute(name);  
    		attribute.setValue(value);
    	}
    	//element.add(attribute);
    	try {
    		XmlDom4jUtils.saveDocumentToFile(Const.SYSTEM_TABLES_PATH, document);
    	} catch (Exception e) {
    		e.printStackTrace();
    	}
    	listTables();
    }
    public void deleteTables(){
    	String id = getPara(0);
    	String name = getPara(1);
    	String modelName = getAttrForStr("modelName");
    	Document document = XmlDom4jUtils.getDocument(Const.SYSTEM_TABLES_PATH);
    	Element element = document.getRootElement();  
    	List<Element> listElements = document.selectNodes("/"+modelName+"s/"+name+"[@id='"+id+"']");
    	Element delelement = null;
		if(listElements!=null && listElements.size()>0){
    		delelement = listElements.get(0);
    	}
    	element.remove(delelement);
    	try {
			XmlDom4jUtils.saveDocumentToFile(Const.SYSTEM_TABLES_PATH, document);
		} catch (Exception e) {
			e.printStackTrace();
		}
    	listTables();
    }
    public void deleteFields(){
    	String id = getPara(0);
    	String name = getPara(1);
    	String modelName = getAttrForStr("modelName");
    	Document document = XmlDom4jUtils.getDocument(Const.SYSTEM_TABLES_PATH);
    	Element element = document.getRootElement();
    	List<Element> listElements = document.selectNodes("/"+modelName+"s/"+name+"[@id='"+id+"']");
    	Element delelement = null;
		if(listElements!=null && listElements.size()>0){
    		delelement = listElements.get(0);
    	}
    	element.remove(delelement);
//    	
//    	
//    	
//    	element = element.element(v1);
//    	element.remove(element.element(v0));
    	try {
			XmlDom4jUtils.saveDocumentToFile(Const.SYSTEM_TABLES_PATH, document);
		} catch (Exception e) {
			e.printStackTrace();
		}
    	listTables();
    }
}
